package com.zmykj.hik.hikService.Common;

import com.alibaba.fastjson.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author LengChen
 * @version 1.0
 * @date 2021/4/30
 */
public class HikStringUtils {


    /**
     * 组装海康中文数据（JSON中文需要特殊转数组组装，所以这样操作）
     * @param str 原字符串
     * @param name 中文字符串
     * @return
     */
    public static CommonClass.StringPointer hikStringZ(String str, String name){
        //组装海康数据，海康数据中文需要特殊转换
        String[] userInfoArr = str.split(name);
        byte[] NameByteArr = new byte[0];
        try {
            NameByteArr = name.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        int iStringSize = NameByteArr.length + userInfoArr[0].length() + userInfoArr[1].length();

        CommonClass.StringPointer pInbound = new CommonClass.StringPointer(iStringSize);
        System.arraycopy(userInfoArr[0].getBytes(), 0, pInbound.sData, 0, userInfoArr[0].length());
        System.arraycopy(NameByteArr, 0, pInbound.sData, userInfoArr[0].length(), NameByteArr.length);
        System.arraycopy(userInfoArr[1].getBytes(), 0, pInbound.sData, userInfoArr[0].length() + NameByteArr.length, userInfoArr[1].length());
        pInbound.write();
        return pInbound;
    }

    /**
     * 判断字符串中是否包含中文
     * @param str
     * 待校验字符串
     * @return 是否为中文
     * @warn 不能校验是否为中文标点符号
     */
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }


    /**
     * map转xml格式(多结构拼接生成)
     * @param sortedMap
     * @return
     */
    public static String mapToSignXml(SortedMap<Object, Object> sortedMap) {
        StringBuilder builder = new StringBuilder();
        Iterator iterator = sortedMap.keySet().iterator();
        while (iterator.hasNext()) {
            Object key = (String) iterator.next();
            Object value = sortedMap.get(key);
            builder.append("<" + key + ">");
            if (value instanceof SortedMap) {
                SortedMap sortedMap1 = (SortedMap) value;
                builder.append(mapToSignXml(sortedMap1));
            } else {
                builder.append(value);
            }
            builder.append("</" + key + ">");

        }
        return builder.toString();
    }

    /**
     * XML转MAP
     * @param Xml
     * @return
     */
    public static Map XmlToMap(String Xml){
        SAXReader saxReader = new SAXReader();
        Document document;
        try {
            document = saxReader.read(new ByteArrayInputStream(Xml.getBytes()));
            Map map = ParseXMLUtils.Dom2Map(document);
            return map;
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {
        SortedMap<Object, Object> requestMap = new TreeMap<Object, Object>();
        SortedMap<Object, Object> requestMap2 = new TreeMap<Object, Object>();
        requestMap2.put("userId","75b843e14dd748f29a4b2add68ff6006");
        requestMap2.put("userName","张力");
        requestMap2.put("deviceID","F80936533");
        requestMap2.put("ConfigParam3","0");
        requestMap2.put("ConfigParam4","0");
        requestMap.put("Params",requestMap2);
        List<Map> list = new ArrayList<>();
        list.add(requestMap2);
        System.out.println(JSONObject.toJSONString(list));
        System.out.println(HikStringUtils.mapToSignXml(requestMap));
    }

    /*public static void main(String[] args) {

        SortedMap<Object, Object> requestMap = new TreeMap<Object, Object>();

        SortedMap<Object, Object> paramMap = new TreeMap<Object, Object>();
        paramMap.put("requid", "111");

        SortedMap<Object, Object> retrieveargsMap = new TreeMap<Object, Object>();

        retrieveargsMap.put("code1", 111);
        retrieveargsMap.put("code2", 222);

        SortedMap<Object, Object> paramMap1 = new TreeMap<Object, Object>();
        paramMap1.put("child1", "1111");
        paramMap1.put("child2", "2222");
        paramMap1.put("child3", "3333");
        retrieveargsMap.put("code3", paramMap1);

        paramMap.put("retriev", retrieveargsMap);

        requestMap.put("request",paramMap);

        String soapXML = mapToSignXml(requestMap);
        System.out.println(soapXML);

    }*/

}
